package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import com.itheima.common.ThreadLocalUtil;
import com.itheima.dto.ShoppingCarDto;
import com.itheima.mapper.ShoppingCarMapper;
import com.itheima.pojo.ShoppingCar;
import com.itheima.service.ShoppingCarService;
import com.itheima.vo.ShoppingCarVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :WuJunJie
 * packageName :com.itheima.service.impl
 * fileName :ShopingCarServiceImpl
 * creationTime :2022-11-27 16:34
 */
@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {
    @Autowired
    private ShoppingCarMapper shoppingCarMapper;
    @Override
    public ShoppingCarVo insertShoppingCar(ShoppingCarDto shoppingCarDto) {
        ShoppingCar shoppingCar = new ShoppingCar();
        BeanUtils.copyProperties(shoppingCarDto,shoppingCar);
        shoppingCar.setId(IdUtil.getSnowflakeNextIdStr());
        shoppingCar.setUserId(ThreadLocalUtil.getid());
        shoppingCar.setCreateTime(LocalDateTime.now());
        ShoppingCar shoppingCarInDB=shoppingCarMapper.selectByDishIdOrSetmealId(shoppingCar);
        ShoppingCarVo shoppingCarVo = new ShoppingCarVo();
        if (shoppingCarInDB!=null){
            shoppingCarInDB.setNumber(shoppingCarInDB.getNumber()+1);
            Integer update=shoppingCarMapper.updatebyId(shoppingCarInDB);
            if (update>0){
                BeanUtils.copyProperties(shoppingCarInDB,shoppingCarVo);
                return shoppingCarVo;
            }
        }
        shoppingCar.setNumber(1);
        Integer insert=shoppingCarMapper.insertShoppingCar(shoppingCar);
        if (insert>0){
            BeanUtils.copyProperties(shoppingCar,shoppingCarVo);
            return shoppingCarVo;
        }
        return null;
    }

    @Override
    public List<ShoppingCarVo> selectShoppingCar() {
        List<ShoppingCar> shoppingCars=shoppingCarMapper.selectShoppingCarByUserId(ThreadLocalUtil.getid());
        List<ShoppingCarVo> shoppingCarVos = new ArrayList<>();
        for (ShoppingCar shoppingCar : shoppingCars) {
            ShoppingCarVo shoppingCarVo = new ShoppingCarVo();
            BeanUtils.copyProperties(shoppingCar,shoppingCarVo);
            shoppingCarVos.add(shoppingCarVo);
        }
        return shoppingCarVos;
    }

    @Override
    public ShoppingCarVo deleteShoppingCar(ShoppingCarDto shoppingCarDto) {
        ShoppingCar shoppingCar = new ShoppingCar();
        BeanUtils.copyProperties(shoppingCarDto,shoppingCar);
        shoppingCar.setId(IdUtil.getSnowflakeNextIdStr());
        shoppingCar.setUserId(ThreadLocalUtil.getid());
        shoppingCar.setCreateTime(LocalDateTime.now());
        ShoppingCar shoppingCarInDB=shoppingCarMapper.selectByDishIdOrSetmealId(shoppingCar);
        ShoppingCarVo shoppingCarVo = new ShoppingCarVo();
        if (shoppingCarInDB.getNumber()>1){
            shoppingCarInDB.setNumber(shoppingCarInDB.getNumber()-1);
            Integer integer = shoppingCarMapper.updatebyId(shoppingCarInDB);
            if (integer>0){

                BeanUtils.copyProperties(shoppingCarInDB,shoppingCarVo);
                return shoppingCarVo;
            }
            return null;
        }
        Integer delete=shoppingCarMapper.deleteShoppingCar(shoppingCar);
        if (delete>0){
            BeanUtils.copyProperties(shoppingCarInDB,shoppingCarVo);
            shoppingCarVo.setNumber(0);
            return shoppingCarVo;
        }
        return null;
    }
    @Override
    public Boolean deleteAllShoppingCar() {
        Integer delete=shoppingCarMapper.deleteAllShoppingCar(ThreadLocalUtil.getid());
        return delete>0;
    }

    @Override
    public List<ShoppingCar> selectListByUserId() {
        List<ShoppingCar> shoppingCars = shoppingCarMapper.selectShoppingCarByUserId(ThreadLocalUtil.getid());
        return shoppingCars;
    }

}
